package cloud.xiangyixiang.www.spring.boot.java.basics.controller;

import cloud.xiangyixiang.www.spring.boot.java.basics.pojo.bo.BookConsumer;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * ConsumerController
 * </p>
 *
 * @author zhixinglu
 * @date 2023/9/21 14:22
 */
public class ConsumerController {

    public static void main(String[] args) {
        test1();

        test2();
    }

    /**
     * consumer接口
     */
    private static void test1() {
        String[] arr = {"张三:18", "李四:25", "王五:28"};
        consumerTest(arr, con -> {
            System.out.print("姓名：" + con.split(":")[0] + "，");
        }, con -> {
            System.out.println("年龄：" + con.split(":")[1]);
        });
    }

    private static void consumerTest(String[] arr, Consumer<String> consumer1, Consumer<String> consumer2) {
        for(int i = 0; i<arr.length; i++) {
            consumer1.andThen(consumer2).accept(arr[i]);
        }
    }

    private static void test2() {
        List<BookConsumer> list = new ArrayList<>();
        list.add(new BookConsumer("effective java", 110));
        list.add(new BookConsumer("effective c++", 88));
        list.add(new BookConsumer("effective c#", 59));

        list.forEach(x -> {
            if(x.getBookName().contains("java")) {
                x.setPrice(x.getPrice()/2);
            }
        });
        System.out.println(list);
    }

}
